$(function () {
    getCode();
    getRoleList();
})

function editUser(data, index) {
    layui.use(['upload', 'form', 'laydate', 'layer'], function () { //此段代码必不可少
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#entranceTime'
        });
        //执行实例
        var uploadInst = upload.render({
            elem: '#icon_change', //绑定元素

            url: LOCALHOSTURL + '/user/upload', //上传接口

            accept: 'images',
            acceptMime: 'image/*',
            xhrFields: {
                withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
            },
            headers: {
                "Authorization": $.cookie("token")
            },
            crossDomain: true,
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#icon').attr('src', result); //图片链接（base64）
                });
            },
            error: function () {
            }
        });
        if (data == '') {
            $("#code").removeAttr("readonly");
            $("#update").html("添加");
            $("#update").click(function () {
                let code = $("#code").val();
                let password = $("#password").val();
                let cfmpsd = $("#cfmpassword").val();
                if (code == '') {
                    layer.msg("用户编号不能为空", function () {
                        $("#code").focus();
                    })
                } else if (code != '') {
                    let result = myAjax("/user/user/getUserByName", "get", {"username": code});
                    if (result.data == null) {
                        if (password == '') {
                            layer.msg("用户密码不能为空", function () {
                                $("#password").focus();
                            })
                        } else if (password != cfmpsd) {
                            layer.msg("两次密码输入不一致", function () {
                                $("#password").focus();
                            })
                        } else {
                            let userdata = {
                                "code": code,
                                "name": $("#name").val(),
                                "password": password,
                                "phone": $("#phone").val(),
                                "sex": $("#sex").find("option:selected").val(),
                                "graduate": $("#graduate").find("option:selected").val(),
                                "role": $("#role").find("option:selected").val(),
                                "entranceTime": $("#entranceTime").val(),
                                "icon": $("#icon").attr("src")
                            };
                            console.log(userdata);
                            let result = ajaxModel("/user/user/register", "post", userdata);
                            if (result.code == 200) {
                                layer.alert(result.message, {icon: 1}, function () {
                                    parent.location.reload();
                                    parent.layer.close(index);
                                });
                            } else
                                layer.alert(result.message, {icon: 1});
                        }
                    } else {
                        layer.msg("该用户编号已存在");
                        $("#code").focus();
                    }
                }
            });
            $("#cancel").click(function () {
                parent.layer.close(index);
            });
            form.render();
        } else {
            //给表单赋值
            $("#code").val(data.code);
            $("#name").val(data.name);
            // $("#password").val(data.password);
            $("#phone").val(data.phone);
            $("#address").val(data.address);
            $("#sex").find("option[value='" + data.sex + "']").attr("selected", true);
            $("#graduate").find("option[value='" + data.graduate + "']").attr("selected", true);
            $("#role").find("option[value='" + data.role + "']").attr("selected", true);
            $("#entranceTime").val(data.entranceTime);
            $("#icon").attr("src", data.icon);
            $("#update").click(function () {
                let password = $("#password").val();
                let cfmpsd = $("#cfmpassword").val();
                if (password != cfmpsd) {
                    layer.msg("两次密码输入不一致", function () {
                        $("#password").focus();
                    })
                } else {
                    let userdata = {
                        "id": data.id,
                        "code": $("#code").val(),
                        "name": $("#name").val(),
                        "password": $("#password").val(),
                        "phone": $("#phone").val(),
                        "address": $("#address").val(),
                        "sex": $("#sex").find("option:selected").val(),
                        "graduate": $("#graduate").find("option:selected").val(),
                        "role": $("#role").find("option:selected").val(),
                        "entranceTime": $("#entranceTime").val(),
                        "icon": $("#icon").attr("src")
                    };
                    console.log(userdata);
                    let result = ajaxModel("/user/user/updateUser", "post", userdata);
                    if (result.code == 200) {
                        layer.alert(result.message, {icon: 1}, function () {
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else
                        layer.alert(result.message, {icon: 1});
                }
            });
            $("#cancel").click(function () {
                parent.layer.close(index);
            });
            form.render();
        }
    });
}

//获取码表数据
function getCode() {
    let data = null;
    if (sessionStorage.getItem("code") != null) {
        data = JSON.parse(sessionStorage.getItem("code"));
    } else {
        let result = ajaxModel("/user/user/getCodeList", "get");
        data = result.data;
        sessionStorage.setItem("code", JSON.stringify(data));
    }
    console.log(data);
    $("#sex").html("<option value=''>全部</option>");
    $.each(data.sex, function (i, sex) {
        $("#sex").append("<option value='" + sex.code + "'>" + sex.name + "</option>");
    })
    $("#graduate").html("<option value=''>全部</option>");
    $.each(data.graduate, function (i, graduate) {
        $("#graduate").append("<option value='" + graduate.code + "'>" + graduate.name + "</option>");
    })
}

function getRoleList() {
    let data = null;
    if (sessionStorage.getItem("role") != null) {
        data = JSON.parse(sessionStorage.getItem("role"));
    } else {
        let result = ajaxModel("/role/role/getRoleList", "get");
        data = result.data;
        sessionStorage.setItem("role", JSON.stringify(data));
    }
    console.log(data);
    $("#role").html("<option value=''>全部</option>");
    $.each(data, function (i, role) {
        $("#role").append("<option value='" + role.id + "'>" + role.name + "</option>");
    });
}